// pages/user/problem/problem.js
import { getFileType } from '../../utils/file';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    scrollHeight: '100vh',
    questionList: [],
    activeNames: ["1", 0],
  },
  onChange(event) {
    this.setData({
      activeNames: event.detail,
    });
  },
  queryQuestion() {
    getApp().callContainer('/api/sys/question/list', {}, (res) => {
      if (res.data.data.questionList && res.data.data.questionList.length > 0) {
        for (const questionType of res.data.data.questionList) {
          for (const question of questionType.questionList) {
            if (question.imgUrlList) {
              question.imgUrlList = question.imgUrlList.split(',');
            }
          }
        }
        this.setData({questionList: res.data.data.questionList})
        this.loadTmpFile();
      }
    });
  },
  loadTmpFile() {
    for (const questionType of this.data.questionList) {
      for (const question of questionType.questionList) {
        if (question.imgUrlList && question.imgUrlList.length > 0) {
          const tmpUrlList = [];
          for (const imgUrl of question.imgUrlList) {
            console.log('imgUrl:', imgUrl);
            getApp().getTempFilePath(imgUrl).then((res) => {
              const type = getFileType(imgUrl);
              tmpUrlList.push({ url: res, type: type });
              if (tmpUrlList.length == question.imgUrlList.length) {
                question.tmpUrlList = tmpUrlList;
                this.setData({questionList: this.data.questionList })
              }
            })
          }
        }
      }
    }
  },
  onClickPreviewImage(event) {
    const url = event.currentTarget.dataset.operation;
    const urls = [url];
    wx.previewImage({
      current: url,
      urls: urls
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.queryQuestion();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})